package com.tt.service;

import com.tt.common.Const.TtConstans;
import com.tt.common.exception.TtException;
import com.tt.mapper.GoodsCatMapper;
import com.tt.pojo.GoodsCat;
import com.tt.pojo.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 青青
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsCatServiceImpl implements IGoodsCatService
{
    @Autowired
    private GoodsCatMapper goodsCatMapper;

    @Override
    public List<GoodsCat> getCatList(Integer pid)
    {
        List<GoodsCat> catList = goodsCatMapper.getCatList(pid);
        return catList;
    }

    @Override
    public GoodsCat getGoodsCatByID(Integer id)
    {
        GoodsCat catByID = goodsCatMapper.getGoodsCatByID(id);
        return catByID;
    }

    @Override
    public List<GoodsCat> getGoodsCatByPid(Integer id)
    {
        List<GoodsCat> goodsCatByPid = goodsCatMapper.getGoodsCatByPid(id);
        return goodsCatByPid;
    }

    @Override
    public int updateGoodsCat(GoodsCat gc)
    {
        int i = goodsCatMapper.updateGoodsCat(gc);
        return i;
    }

    /**
     * 删除当前节点和所有子节点
     * @param id
     * @return
     */
    @Override
    public int delGoodsCat(Integer id)
    {
        List<Integer> recursion = recursion(id);
        int i = goodsCatMapper.delGoodsCats(recursion);
        return i;
    }

    /**
     * 递归获取当前分类下的所有子分类
     * @param id
     * @return
     */
    private List<Integer> recursion(Integer id)
    {
        ArrayList<Integer> ids = new ArrayList<>();
        List<GoodsCat> catByPid = goodsCatMapper.getGoodsCatByPid(id);
        ids.add(id);
        for (GoodsCat gc : catByPid )
        {
            Boolean isParent = gc.getIsParent();
            Integer cId = gc.getId();
            ids.add(cId);
            if (isParent){
                List<Integer> recursion = recursion(cId);
                ids.addAll(recursion);
            }
        }
        return ids;
    }

    @Override
    public CatVo getCatVo(Integer id)
    {

        List<String> names = byRecursion(id);
        if (StringUtils.isEmpty(names)){
            throw new TtException("没有找到节点");
        }
        Collections.reverse(names);
        StringBuffer buffer = new StringBuffer();
        int size = names.size();
        for (int i = 0; i < size; i++)
        {
            if (i == 0){
                buffer.append(names.get(i));
            }
            else
            {
                buffer.append(TtConstans.xg);
                buffer.append(names.get(i));
            }
            if (i == size){
                buffer.append(names.get(i));
            }
        }
        CatVo catVo = new CatVo();
        catVo.setCid(id);
        catVo.setCatCharinName(buffer.toString());
        catVo.setName(names.get(names.size()-1));
        return catVo;
    }

    /**
     * 递归查找当前节点上的所有父节点
     * @param id
     * @return
     */
    private List<String> byRecursion(Integer id)
    {
        List<String> names = new ArrayList<>();
        GoodsCat catByID = goodsCatMapper.getGoodsCatByID(id);
        if (StringUtils.isEmpty(catByID)){
            throw new TtException("找不到节点ID为："+id+"的节点");
        }
        Integer parentId = catByID.getParentId();
        names.add(catByID.getName());
        if (parentId != 0){
            List<String> pNames = byRecursion(parentId);
            names.addAll(pNames);
        }
        return names;
    }
}
